
	function kjeditor(container) {
		this.container = container;
	}
	
	/**
	 * Replace textarea to iframe
	 */
	kjeditor.prototype.replaceTextarea = function (){
		this.textarea = $(this.container+" textarea");
		this.textarea.css("display","none");
		this.textarea.after("<iframe>");
		this.frame = $(this.container+" iframe");
		this.editorType = 0;
		
		setTimeout(this.init, '1000');
	}
	
	
	/**
	 * Init editor frame
	 */
	kjeditor.prototype.init = function () {
		this.document = this.frame[0].contentDocument;
		this.window = this.frame[0].contentWindow;
		this.document.designMode = 'On';
		this.document.execCommand("useCSS", false, true);
		this.document.body.innerHTML = this.textarea.val();
	}
	
	kjeditor.prototype.switch_edit = function() {
		var html = this.getHtml();
		
		if (!this.editorType) { // frame
			this.frame.css('display','none');
			this.textarea.css('display','');
			this.editorType = 1;
			
			dbg("switch to text");
		}else{ // textarea
			this.textarea.css('display','none');
			this.frame.css('display','');
			this.editorType = 0;
			
			dbg("switch to html");
		}
		this.setHtml(html);
	}
	
	kjeditor.prototype.getHtml = function() {
		if (!this.editorType) { // frame
			return this.document.body.innerHTML;
		}else{ // textarea
			return this.textarea.val();
		}
	}
	kjeditor.prototype.setHtml = function(html) {
		if (!this.editorType) { // frame
			this.document.body.innerHTML = html;
		}else{ // textarea
			this.textarea.val(html);
		}
	}
	kjeditor.prototype.clear = function () {
		dbg('clear');
		html = this.getHtml();
		// Удаляем ненужные теги
		html = html.replace(/<\/?(font|col|meta)(>|\s[^>]*>)/gim, '');
		// А совсем ненужные удаляем с содержимым
		html = html.replace(/<(style|script|frame|iframe|object|embed|title)(>|\s[^>]*>).*?<\/\1>/gim, '');
		// А еще удаляем комментарии 
		html = html.replace(/<!--(.*?)-->/gim, '');
		
		//Чистим атребуты
		html = html.replace(/<(\w+)\s([^>]*)>/gim, function(m, m1, m2){
			var out = '<'+m1+' ';
			if (m2 = m2.match(/\w+=".*?"/gim, '')){
				for (var i=0,l=m2.length ; i<l ; i++){
					if (m2[i].match(/^(href|class|rel|colspan|rowspan)=".+"/gim)){
						out += m2[i];
					}
				}
			}
			return out+'>';
		});
		
		//Удаляем пустые теги
		html = html.replace(/<(p|b|i|strong)\s[^>]*>\s*<\/\1>/gim, '');
		
		//Удаляем лишние пробелы
		html = html.replace(/\s+/gim, ' ');
		
		//Параграфы в таблицах и списках заменяем на <br>
		html = html.replace(/<(li|th|td)(>|\s[^>]*>)(.*?)<\/\1>/gim, function (m){
			m = m.replace(/<p(>|\s[^>]*>)/gim, '');
			return 	m.replace(/<\/p(>|\s[^>]*>)/gim, '<br>');
		});
		this.setHtml(html);
	}
	
	kjeditor.prototype.format = function() {
	}
	
	kjeditor.prototype.surroundSelection = function(tagName, attr) {
		var range, root, start, end
		var selection = this.window.getSelection()
		range = selection.getRangeAt(0)
		
		range.surroundContents(this.document.createElement(tagName));
	}
	
	$(function(){
		editor = new kjeditor(".kjeditor_container");
		editor.replaceTextarea();
	});
	
	function dbg(msg) {
		document.getElementById("dbg").innerHTML = msg;
	}
